﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using taigu.WeChat;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace taigu
{
    /*地址接口目前只提供一个添加接口，只能添加一次*/
    public class AddressService : ApplicationService, IAddressService
    {
        private readonly IRepository<Address, Guid> _todoItemRepository;
        public AddressService(IRepository<Address, Guid> todoItemRepository)
        {
            _todoItemRepository = todoItemRepository;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        [HttpPost, Route("/wechat/createAddress")]
        public async Task<ResData<string>> CreateAddress(CreateAddressDto address)
        {
            try
            {
               
                var model = _todoItemRepository.FirstOrDefault(x => x.user_id == address.user_id);
                if (model == null)
                {
                    await _todoItemRepository.InsertAsync(ObjectMapper.Map<CreateAddressDto, Address>(address));
                    return new ResData<string>() { Status = ResStatus.成功, Msg = "sucess", Info = "" };
                }
                else
                {
                    return new ResData<string>() { Status = ResStatus.failed, Msg = "failed", Info = "当前用户存在收货地址" };
                }
            }
            catch (Exception ex)
            {
                return new ResData<string>() { Status = ResStatus.failed, Msg = ex.Message, Info = "" };
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("/wechat/deleteAddress")]
        public async Task<ResData<string>> AddressDelete(Guid id)
        {
            await _todoItemRepository.DeleteAsync(id);
            return new ResData<string>() { Status = ResStatus.成功, Msg = "sucess", Info = "" };
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("/wechat/updateAddress")]
        public async Task<AddressDto> AddressFill(Guid id)
        {
            var item = await _todoItemRepository.FindAsync(id);

            return ObjectMapper.Map<Address, AddressDto>(item);
        }

        /// <summary>
        /// 显示方法
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("/wechat/getAddressList")]
        public async Task<List<AddressDto>> AddressShow()
        {
            var items = await _todoItemRepository.GetListAsync();

            return ObjectMapper.Map<List<Address>, List<AddressDto>>(items);
        }


    }
}
